#include <stdint.h>
#include "config.h"
#include "apitrans.h"
#include "result.h"

int api_prn_init(unsigned char *cmd, int len)
{
	uint16_t ret = DEAL_SUCCESS;
	if(len != 0){
		ret = ERROR_PARAM;
		memcpy(cmd,&ret,2);
		return -1;
	}
	if(prn_init()<0){
		ret = ERR_PRN_INIT;
	}
	memcpy(cmd,&ret,2);
	return 2;
}

int api_set_prn_font(unsigned char *cmd, int len)
{
	uint16_t ret = DEAL_SUCCESS;
	int offset = 0;
	uint8_t hz_font = cmd[offset++];
	uint8_t zm_font = cmd[offset++];
	if(len != offset){
		ret = ERROR_PARAM;
		memcpy(cmd,&ret,2);
		return -1;
	}
	
	if(prn_set_font(hz_font, zm_font)<0){
		ret = ERR_SET_PRN_FONT;
	}
	memcpy(cmd,&ret,2);
	return 2;
}

static int api_prn_get_status(unsigned char *cmd, int len)
{
	uint16_t ret = DEAL_SUCCESS;
	uint8_t status = 0;
	uint8_t arg = 0;
	uint8_t offset = 0;
	if(len != 0){
		ret = ERROR_PARAM;
		memcpy(cmd,&ret,2);
		return -1;
	}
	if(prn_get_status(&arg)<0){
		ret = ERR_PRN_GET_STATUS;
		memcpy(cmd,&ret,2);
		return -1;
	}
	memcpy(cmd,&ret,2);
	offset += 2;
	cmd[offset] = arg;
	offset += 1;
	return offset;
}

static int api_prn_set_grey(unsigned char *cmd, int len)
{
	uint16_t ret = DEAL_SUCCESS;
	uint8_t grey = 0;
	uint8_t offset = 0;
	
	grey = cmd[offset++];
	if(len != offset){
		ret = ERROR_PARAM;
		memcpy(cmd,&ret,2);
		return -1;
	}
	if(prn_set_grey(grey)<0){
		ret = ERR_PRN_SET_GREY;
	}
	offset = 0;
	memcpy(cmd,&ret,2);
	offset += 2;
	return offset;
}

static int api_prn_string(unsigned char *cmd, int len)
{
	uint16_t ret = DEAL_SUCCESS;
	uint32_t arg = 0;
	uint16_t offset = 0;
	uint16_t prn_size;
	uint8_t prn_str[MAX_SIZE]={0};

	prn_size = BCDtoDec(cmd+offset,2);
	offset += 2;
	if(prn_size > MAX_SIZE){
		ret = ERROR_PARAM;
		memcpy(cmd,&ret,2);
		return -1;
	}
	memcpy(prn_str, cmd+offset,prn_size);
	offset += prn_size; 
	if(offset != len){
		ret = ERROR_PARAM;
		memcpy(cmd,&ret,2);
		return -1;
	}
	if(prn_string(prn_str)<0){
		ret = ERR_PRN_STRING;
	}
	offset = 0;
	memcpy(cmd,&ret,2);
	offset += 2;
	return offset;
}

static int api_prn_image(unsigned char *cmd, int len)
{
	uint16_t ret = DEAL_SUCCESS;
	uint32_t arg = 0;
	uint16_t offset = 0;
	
	uint16_t w;
	uint16_t h;
	uint8_t x_pos;
	uint8_t image_buf[1024]={0};
	uint16_t buf_len;
	w = BCDtoDec(cmd+offset,2);
	offset += 2;
	h = BCDtoDec(cmd+offset,2);
	offset += 2;
	x_pos = cmd[offset++];
	buf_len = BCDtoDec(cmd+offset,2);
	offset += 2;
	if(buf_len > 1024){
		ret = ERROR_PARAM;
		memcpy(cmd,&ret,2);
		return -1;
	}
	memcpy(image_buf, cmd+offset,buf_len);
	offset += buf_len;
	if(offset != len){
		ret = ERROR_PARAM;
		memcpy(cmd,&ret,2);
		return -1;
	}
	if(prn_image(w, h, x_pos, image_buf)< 0){
		ret = ERR_PRN_IMG;
	}
	memcpy(cmd,&ret,2);
	offset += 2;
	return offset;
}


static int api_set_prn_format(unsigned char *cmd, int len)
{
	uint8_t offset = 0;
	uint8_t border = 0;
	uint8_t column = 0;
	uint8_t row = 0;
	uint16_t ret = DEAL_SUCCESS;
	
	border = cmd[offset++];
	column = cmd[offset++];
	row = cmd[offset++];
	if(offset != len){
		ret = ERROR_PARAM;
		memcpy(cmd,&ret,2);
		return -1;
	}
	if(prn_set_spacing(border, column, row) < 0){
		ret = ERR_PRN_FORMAT;
	}
	offset = 0;
	memcpy(cmd,&ret,2);
	offset += 2;
	return offset;
}
static int api_prn_underline(unsigned char *cmd, int len)
{
	uint8_t offset = 0;
	int tmp_h =0;
	uint16_t ret = DEAL_SUCCESS;
	uint8_t mode = cmd[offset++];
	if(offset != len){
		ret = ERROR_PARAM;
		memcpy(cmd,&ret,2);
		return -1;
	}
	if(prn_set_underline(mode) < 0){
		ret = ERR_PRN_FORMAT;
	}
	offset = 0;
	memcpy(cmd,&ret,2);
	offset += 2;
	return offset;
}

int api_prn_start(unsigned char *cmd, int len)
{
	uint16_t ret = DEAL_SUCCESS;
	if(prn_start()<0){
		ret = ERR_PRN_START;
	}
	memcpy(cmd,&ret,2);
	return 2;
}


/*int api_prn_exit(unsigned char *cmd, int len)
{
	uint16_t ret = DEAL_SUCCESS;
	if(prn_exit()<0){
		ret = ERR_PRN_EXIT;
	}
	memcpy(cmd,&ret,2);
	return 2;
}*/


static func_t print_fun[] = {
	NULL, api_prn_init, api_set_prn_font,api_prn_get_status,\
		api_prn_set_grey,api_prn_string,api_prn_image,api_set_prn_format,\
		api_prn_underline,api_prn_start,//api_prn_exit
};

int cmd_print(uint8_t cmd, char *buf, int len)
{
	if(cmd < 0x01 || cmd > FUN_SIZE(print_fun))
		return -5;
	return print_fun[cmd](buf,len);
}

